#include <string>

#include "RingQueue.hpp"

static int in = 1;

struct pthread_name
{
    RingQueue<int> *rq;
    std::string _name;
};

void *production(void *arg)
{
    pthread_name *p = static_cast<pthread_name *>(arg);
    while (true)
    {
        sleep(1);
        p->rq->EQueue(in);
        std::cout << p->_name << "生产一个任务" << ":" << in << std::endl;
        ++in;

    }

    return nullptr;
}

void *consumer(void *arg)
{
    pthread_name *p = static_cast<pthread_name *>(arg);
    while (true)
    {
        sleep(1);
        int rvalue = p->rq->Pop();

        std::cout << p->_name << "消费一个任务" << ":" << rvalue << std::endl;
    }

    return nullptr;
}

int main()
{
    pthread_t P[3], C[2]; // 三生产，两消费

    RingQueue<int> *rq = new RingQueue<int>;

    struct pthread_name *td1 = new pthread_name;
    td1->rq = rq;
    td1->_name = "thread1";
    pthread_create(&P[0], nullptr, production, td1);

    struct pthread_name *td2 = new pthread_name;
    td2->rq = rq;
    td2->_name = "thread2";
    pthread_create(&P[1], nullptr, production, td2);

    struct pthread_name *td3 = new pthread_name;
    td3->rq = rq;
    td3->_name = "thread3";
    pthread_create(&P[2], nullptr, production, td3);

    struct pthread_name *td4 = new pthread_name;
    td4->rq = rq;
    td4->_name = "thread4";
    pthread_create(&C[0], nullptr, consumer, td4);

    struct pthread_name *td5 = new pthread_name;
    td5->rq = rq;
    td5->_name = "thread5";
    pthread_create(&C[1], nullptr, consumer, td5);

    pthread_join(P[0], nullptr);
    pthread_join(P[1], nullptr);
    pthread_join(P[2], nullptr);
    pthread_join(C[0], nullptr);
    pthread_join(C[1], nullptr);

    return 0;
}